use actix_web::web::{Data, Json, Query};

use crate::{
    common::{
        app::tags::PERM,
        types::{ApiListResult, ApiResult},
    },
    config::{auth::AuthInfo, state::AppState},
    models::{
        request::{
            common_request::IdRequest,
            perm_req::{PermCreate, PermUpdate},
        },
        response::perm_res::{PermDetail, PermListInfo},
    },
    services::perm_service as PermService,
};

/// 新增权限
#[utoipa::path(
    tags = [PERM],
    post,
    path = "/api/v1/perm/create",
    responses(
        (status = 200, description = "Ok", body = bool),
        (status = 400, description = "Bad Request"),
        (status = 401, description = "Unauthorized"),
        (status = 500, description = "Internal Server Error")
    ),
    request_body = PermCreate
)]
pub async fn create(
    state: Data<AppState>,
    req: Json<PermCreate>,
    auth_info: AuthInfo,
) -> ApiResult<bool> {
    PermService::create(state, req.into_inner(), auth_info).await
}

/// 删除权限
#[utoipa::path(
    tags = [PERM],
    post,
    path = "/api/v1/perm/delete",
    responses(
        (status = 200, description = "Ok", body = bool),
        (status = 400, description = "Bad Request"),
        (status = 401, description = "Unauthorized"),
        (status = 500, description = "Internal Server Error")
    ),
    request_body = IdRequest
)]
pub async fn delete(
    state: Data<AppState>,
    req: Json<IdRequest>,
    auth_info: AuthInfo,
) -> ApiResult<bool> {
    PermService::delete(state, req.into_inner(), auth_info).await
}

/// 修改权限
#[utoipa::path(
    tags = [PERM],
    post,
    path = "/api/v1/perm/update",
    responses(
        (status = 200, description = "Ok", body = bool),
        (status = 400, description = "Bad Request"),
        (status = 401, description = "Unauthorized"),
        (status = 500, description = "Internal Server Error")
    ),
    request_body = PermUpdate
)]
pub async fn update(
    state: Data<AppState>,
    req: Json<PermUpdate>,
    auth_info: AuthInfo,
) -> ApiResult<bool> {
    PermService::update(state, req.into_inner(), auth_info).await
}

/// 权限详情
#[utoipa::path(
    tags = [PERM],
    get,
    path = "/api/v1/perm/detail",
    responses(
        (status = 200, description = "Ok", body = PermDetail),
        (status = 400, description = "Bad Request"),
        (status = 401, description = "Unauthorized"),
        (status = 500, description = "Internal Server Error")
    ),
    request_body = IdRequest
)]
pub async fn detail(
    state: Data<AppState>,
    req: Query<IdRequest>,
    auth_info: AuthInfo,
) -> ApiResult<PermDetail> {
    PermService::detail(state, req.into_inner(), auth_info).await
}

/// 权限列表
#[utoipa::path(
    tags = [PERM],
    get,
    path = "/api/v1/perm/list",
    responses(
        (status = 200, description = "Ok", body = PermListInfo),
        (status = 400, description = "Bad Request"),
        (status = 401, description = "Unauthorized"),
        (status = 500, description = "Internal Server Error")
    ),
)]
pub async fn list(state: Data<AppState>, auth_info: AuthInfo) -> ApiListResult<PermListInfo> {
    PermService::list(state, auth_info).await
}
